<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 网吧管理
 * @filename admin_netbar.php
 * @author hmw
 *
 */
class Admin_netbar extends MY_Controller {

	function Admin_netbar()
	{
		parent::MY_Controller();
		parent::check_admin_login();
	}


    function bar_admin(){
        self::bar_list();
    }

    function bar_list($area_id=0,$offset=0){
        $var = array();
        $this->load->model('Mnetbar');
        $temp = $this->Mnetbar->get_areas();
        $areas = array();
        foreach($temp as $area){
            $areas[$area['area_id']] = $area['area_name'];
        }
        if($area_id == 0){
            $area_id = $temp[0]['area_id'];
        }
        $var['areas'] = $areas;
        $var['area_id'] = $area_id;
        $total = $this->Mnetbar->get_netbar_count($area_id);

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_netbar/bar_list/'.$area_id.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;

		$limit = $config['per_page'];

        $bars = $this->Mnetbar->get_netbar_by_area($area_id,$offset,$limit);
        $var['bars'] = $bars;
        $this->template->view('admin/bar_list',$var);
    }

    function bar_new($area_id=0){
        $var = array();
        $this->load->model('Mnetbar');
        $this->load->model('Magent');
        $temp = $this->Mnetbar->get_areas();
        $areas = array();
        foreach($temp as $area){
            $areas[$area['area_id']] = $area['area_name'];
        }
        $area_id = $temp[0]['area_id'];
        $agents = $this->Magent->get_agents($area_id,0,20);
        $var['areas'] = $areas;
        $var['agents'] = $agents;
        $var['area_id'] = $area_id;
        $this->template->view('admin/bar_add',$var);
    }

    function bar_add(){
		$config['upload_path'] = $this->config->item('upload_uri').'netbar/';
		if(!file_exists($config['upload_path'])){
			mkdir($config['upload_path']);
			chmod($config['upload_path'],0777);
		}
		$config['allowed_types'] = $this->config->item('bar_img_types');
		$config['max_size'] = $this->config->item('bar_img_max_size');
		$config['max_width'] = $this->config->item('bar_img_width');
		$config['max_height'] = $this->config->item('bar_img_height');
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('bar_logo')) {
			echo '文件上传失败，可能原因为：<br />'.$this->upload->display_errors();
		} else {
            $bar_uid = $this->input->post('bar_uid') ? $this->input->post('bar_uid') : 'NB000000';
            $agent_id = $this->input->post('agent_id') ? $this->input->post('agent_id') : '0';
            $pc_num = $this->input->post('pc_num') ? $this->input->post('pc_num') : '0';
			$bar_name = $this->input->post('bar_name') ? $this->input->post('bar_name') : '未来网吧';
            $bar_charge = $this->input->post('bar_charge') ? $this->input->post('bar_charge') : '阿努克丝';
            $area_id = $this->input->post('area_id') ? $this->input->post('area_id') : '1';
			$bar_addr = $this->input->post('bar_addr') ? $this->input->post('bar_addr') : '火星村一号';
            $bar_mobile = $this->input->post('bar_mobile') ? $this->input->post('bar_mobile') : '88888888';
			$bar_tel = $this->input->post('bar_tel') ? $this->input->post('bar_tel') : '88888888';
			$bar_link = $this->input->post('bar_link') ? $this->input->post('bar_link') : '#';
			$data = $this->upload->data();
			$bar_logo = $data['file_name'];
			$values = array('bar_name'=>$bar_name,'bar_charge'=>$bar_charge,'area_id'=>$area_id,'bar_addr'=>$bar_addr,'bar_tel'=>$bar_tel,'bar_mobile'=>$bar_mobile,'bar_link'=>$bar_link,'bar_logo'=>$bar_logo,'bar_uid'=>$bar_uid,'agent'=>$agent_id,'pc_num'=>$pc_num);
			$this->load->model('Mnetbar');
			if ($this->Mnetbar->insert_new_bar($values)) {
				echo '银行网吧成功！&nbsp;&nbsp;<a href="/admin_netbar/bar_new/">继续添加</a>&nbsp;&nbsp;<a href="/admin_netbar/bar_list/">查看列表</a>';
			} else {
				echo '银行网吧失败！&nbsp;&nbsp;<script type="text/javascript"><a href="javascript:window.history.back();">返回</a></script>';
			}
		}
	}

    function bar_edit($id=''){
        if($id === ''){
            echo '选中网吧后方可继续操作';exit;
        }
        $var = array();
        $this->load->model('Mnetbar');
        $temp = $this->Mnetbar->get_areas();
        $areas = array();
        foreach($temp as $area){
            $areas[$area['area_id']] = $area['area_name'];
        }
        $var['areas'] = $areas;
        $info = $this->Mnetbar->get_bar_info($id);
        $area_id = $info['area_id'];
        $this->load->model('Magent');
        $agents = $this->Magent->get_agents($area_id,0,20);
        $var['info'] = $info;
        $var['agents'] = $agents;
        $this->template->view('admin/bar_edit',$var);
    }

    function bar_modify(){
        $id = $this->input->post('id');
        $bar_uid = $this->input->post('bar_uid') ? $this->input->post('bar_uid') : 'NB000000';
        $agent_id = $this->input->post('agent_id') ? $this->input->post('agent_id') : '0';
        $pc_num = $this->input->post('pc_num') ? $this->input->post('pc_num') : '0';
        $bar_name = $this->input->post('bar_name') ? $this->input->post('bar_name') : '未来网吧';
        $bar_charge = $this->input->post('bar_charge') ? $this->input->post('bar_charge') : '阿努克丝';
        $area_id = $this->input->post('area_id') ? $this->input->post('area_id') : '1';
		$bar_addr = $this->input->post('bar_addr') ? $this->input->post('bar_addr') : '火星村一号';
        $bar_mobile = $this->input->post('bar_mobile') ? $this->input->post('bar_mobile') : '88888888';
		$bar_tel = $this->input->post('bar_tel') ? $this->input->post('bar_tel') : '88888888';
		$bar_link = $this->input->post('bar_link') ? $this->input->post('bar_link') : '#';
        $values = array('bar_name'=>$bar_name,'bar_charge'=>$bar_charge,'area_id'=>$area_id,'bar_addr'=>$bar_addr,'bar_mobile'=>$bar_mobile,'bar_tel'=>$bar_tel,'bar_link'=>$bar_link,'agent'=>$agent_id,'pc_num'=>$pc_num,'bar_uid'=>$bar_uid);
        //处理上传
        $config['upload_path'] = $this->config->item('upload_uri').'netbar/';
		if(!file_exists($config['upload_path'])){
			mkdir($config['upload_path']);
			chmod($config['upload_path'],0777);
		}
		$config['allowed_types'] = $this->config->item('bar_img_types');
		$config['max_size'] = $this->config->item('bar_img_max_size');
		$config['max_width'] = $this->config->item('bar_img_width');
		$config['max_height'] = $this->config->item('bar_img_height');
		$this->load->library('upload', $config);
		if ($this->upload->do_upload('bar_logo')) {
			$data = $this->upload->data();
			$bar_logo = $data['file_name'];
            $values['bar_logo'] = $bar_logo;
		}

        $this->load->model('Mnetbar');
        if($this->Mnetbar->update_bar_info($id,$values)){
            echo '信息修改成功！';
        } else {
            echo '修改失败！';
        }
    }

    function bar_del($id=''){
        if($id === ''){
            echo '选中网吧后方可继续操作';exit;
        }
        $this->load->model('Mnetbar');
        if($this->Mnetbar->delete_bar($id)){
            echo '网吧删除成功！';
        } else {
            echo '删除失败！';
        }
    }

    function area_list($offset=0){
        $var = array();
        $this->load->model('Mnetbar');

        $total = $this->Mnetbar->get_area_count();

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_netbar/area_list/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;

		$limit = $config['per_page'];
        $areas = $this->Mnetbar->get_areas($limit,$offset);

        $var['areas'] = $areas;
        $this->template->view('admin/area_list',$var);
    }

    function area_new(){
        $this->template->view('admin/area_add');
    }

    function area_add(){
        $area_name = $this->input->post('area_name');
        if(!$area_name){
            echo '必须填写地区名';exit;
        }
        $this->load->helper('pinyin');
        $en_name = Pinyin($area_name,'utf8');
        $values = array('area_name'=>$area_name,'en_name'=>$en_name);
        $this->load->model('Mnetbar');
        if($this->Mnetbar->insert_new_area($values)){
            echo '添加地区成功！';
        } else {
            echo '添加失败！';
        }
    }

    function area_edit($id){
        if($id === ''){
            echo '选中地区后方可继续操作';exit;
        }
        $this->load->model('Mnetbar');
        $info = $this->Mnetbar->get_area_info($id);
        $this->template->view('admin/area_edit',$info);
    }

    function area_modify(){
        $area_id = $this->input->post('area_id');
        $area_name = $this->input->post('area_name');
        if(!$area_name){
            echo '必须填写地区名';exit;
        }
        $this->load->helper('pinyin');
        $en_name = Pinyin($area_name,'utf8');
        $values = array('area_name'=>$area_name,'en_name'=>$en_name);
        $this->load->model('Mnetbar');
        if($this->Mnetbar->update_area_info($area_id,$values)){
            echo '地区修改成功！';
        } else {
            echo '修改失败！';
        }
    }

    function area_del($id){
        if($id === ''){
            echo '选中地区后方可继续操作';exit;
        }
        $this->load->model('Mnetbar');
        if($this->Mnetbar->delete_area($id)){
            echo '地区删除成功！';
        } else {
            echo '删除失败！';
        }
    }



}

?>
